$(document).ready(function(){
	Role_Power_Js.init();
});

/**
 * 角色权限js
 * author lxw
 * 2018/01/23
 */
var Role_Power_Js={
	/**
	 * 序号
	 */
	ROLEID_ : "",
	/**
	 * 模块树对象
	 */
	MODULEOBJ_:{},
	/**
	 * 用户树对象
	 */
	USEROBJ_ : {},
	
	/**
	 * 初始化
	 */
	init:function(){
		Role_Power_Js.ROLEID_ = GetRequest().roleId;
		Role_Power_Js.findRolePowerById(Role_Power_Js.ROLEID_);
	},
	/**
	 * 查询角色权限
	 * @param roleId
	 */
	findRolePowerById:function(roleId){
		$.ajax({
		    url:"/crm/role/findRolePowerByUuid",    //请求的url地址
		    dataType:"json",   //返回格式为json
		    async:true,//请求是否异步，默认为异步，这也是ajax重要特性
		    data:{uuid:roleId},    //参数值
		    type:"post",   //请求方式
		    success:function(data){
		    	Role_Power_Js.MODULEOBJ_ = data.module;
		    	Role_Power_Js.USEROBJ_ = data.user;
		    	var obj = data.role;
		    	$("#roleId").val(obj.id);
		    	$("#roleUuid").val(obj.uuid);
		    	$("#roleName").val(obj.roleName);
		    	$("input[name='isUse'][value='"+obj.isUse+"']").attr("checked",true); 
		    	$("#roleRemark").val(obj.roleRemark);
		    	Role_Power_Js.initModuleInfo(data.module);
		    	Role_Power_Js.initUserInfo( data.user);
		    	Role_Power_Js.loadHaving();
		    },
		    error:function(){
		    	$box.promptBox("服务器错误！");
		    }
		});
	},
	/**
	 * 加载功能模块树图
	 * @author George
	 * @param treeData
	 */
	initModuleInfo:function(treeData){
		var zNodes=null;
		//tree的相关设置
		var setting = {
			view: {
				dblClickExpand: true,
				showLine: false,
				selectedMulti: false
			},
			check:{
				enable :true,
				chkStyle:"checkbox",
				nocheckInherit: false
			},
			data: {
				key:{url:"noUrl"},
				simpleData: {
					enable:true,
					idKey: "uuid",
					pIdKey: "puuid",
					rootPId: ""
				}
			},
			callback: {
				onCheck:function (event, treeId, treeNode) {
						var zTree = $.fn.zTree.getZTreeObj("moduleTree");
						var checkedNodes= zTree.getCheckedNodes(true);
						var owner="";
						$.each( checkedNodes, function( key, val ) {
							if(val.id != 0){
								owner=owner+""+val.name + "\r\n";
							}
						} );
						$("#haveModule").val(owner);
					}
			}
		};
		var t = $("#moduleTree");
		t = $.fn.zTree.init(t, setting, treeData);
		var zTree = $.fn.zTree.getZTreeObj("moduleTree");
		zTree.expandAll(true);
	},

	/**
	 * 加载机构用户树图
	 * @author George
	 * @param treeData
	 */
	initUserInfo:function(treeData){
		var zNodes=null;
		//tree的相关设置
		var setting = {
			view: {
				dblClickExpand: true,
				showLine: false,
				selectedMulti: false
			},
			check:{
				enable :true,
				chkStyle:"checkbox",
				nocheckInherit: false
			},
			data: {
				key:{url:"noUrl"},
				simpleData: {
					enable:true,
					idKey: "id",
					pIdKey: "parentId",
					rootPId: ""
				}
			},
			callback: {
				onCheck:function (event, treeId, treeNode) {
						var zTree = $.fn.zTree.getZTreeObj("userTree");
						var checkedNodes= zTree.getCheckedNodes(true);
						var owner="";
						$.each( checkedNodes, function( key, val ) {
							if(val.id != 0){
								owner+=val.name + "\r\n";
							}
						} );
						$("#havaUsers").val(owner);
					}
			}
		};
		var t = $("#userTree");
		t = $.fn.zTree.init(t, setting, treeData);
		var zTree = $.fn.zTree.getZTreeObj("userTree");
		zTree.expandAll(true);
	},

	loadHaving:function(){
		var zTree = $.fn.zTree.getZTreeObj("moduleTree");
		var checkedNodes= zTree.getCheckedNodes(true);
		var owner="";
		$.each( checkedNodes, function( key, val ) {
			if(val.id != 0){
				owner += val.name + "\r\n";
			}
		} );
		$("#haveModule").val(owner);
		
		zTree = $.fn.zTree.getZTreeObj("userTree");
		checkedNodes= zTree.getCheckedNodes(true);
		owner="";
		$.each( checkedNodes, function( key, val ) {
			if(val.id != 0){
				owner += val.name + "\r\n";
			}
		} );
		$("#havaUsers").val(owner);
	},
	/**
	 * 改变权限
	 */
	changePower:function(){
		var title ="确认提示";
		var html = '<div style="padding: 20px 80px 20px;">权限变更后无法恢复，只能再次设置，请慎重！</div>';
		AlertBox.confirm(html, title, Role_Power_Js.sureChangePower, "");
	},

	/**
	 * 确认改变权限
	 */
	sureChangePower:function(){
		var obj = {};
		var zTree1 = $.fn.zTree.getZTreeObj("moduleTree");
		var checkedNodes1= zTree1.getCheckedNodes(true);
		var moduleIds="";
		$.each( checkedNodes1, function( key, val ) {
			if(val.id != 0){
				moduleIds=moduleIds+","+val.uuid;
			}
		} );
		obj.moduleUuids = moduleIds.substring(1)
		
		var zTree = $.fn.zTree.getZTreeObj("userTree");
		var checkedNodes= zTree.getCheckedNodes(true);
		var userIds="";
		$.each( checkedNodes, function( key, val ) {
			if(val.id != 0){
				userIds=userIds+","+val.uuid ;
			}
		} );
		obj.userUuids = userIds.substring(1)
		
		var roleUuid = $("#roleUuid").val();
		obj.roleUuid = roleUuid;
		$.ajax({
		    url:"/crm/role/changeRolePower",    //请求的url地址
		    dataType:"json",   //返回格式为json
		    async:true,//请求是否异步，默认为异步，这也是ajax重要特性
		    data:obj,    //参数值
		    type:"post",   //请求方式
		    success:function(data){
		    	
		    	AlertBox.alert(data.info, "");
		    	if(data.status){
		    		//Role_Power_Js.findRolePowerById(roleUuid);
		    		
		    		AlertBox.onHide(function(){
						parent.closeCurPage();
					})
		    	}
		    },
		    error:function(){
		    	AlertBox.alert("服务器错误！","");
		    }
		});
	},
	/**
	 * 重置
	 */
	restTableData:function(){
		Role_Power_Js.initModuleInfo(Role_Power_Js.MODULEOBJ_);
		Role_Power_Js.initUserInfo(Role_Power_Js.USEROBJ_);
		Role_Power_Js.loadHaving();
	}
}



